import request from '@/utils/http'
import { BaseResponse } from './common'

// 预约状态枚举
export type BookingStatus = 'pending' | 'confirmed' | 'cancelled' | 'rejected' | 'completed'

// 场地类型枚举
export type VenueType = 'family_fishing' | 'lure_fishing' | 'forest_yoga'

// 时间段枚举
export type TimeSlot = 'morning' | 'afternoon' | 'full_day'

// 入场券类型枚举
export type TicketType = 'family_ticket' | 'lure_ticket' | 'yoga_ticket'

// 预约实体接口
export interface Booking {
  _id?: string
  bookingNo: string
  openid: string
  venueType: VenueType
  bookingDate: number // 秒级时间戳
  timeSlot: TimeSlot
  ticketType?: TicketType
  status: BookingStatus
  remark?: string
  cancelReason?: string
  rejectReason?: string
  cancelTime?: number // 秒级时间戳
  rejectTime?: number // 秒级时间戳
  createTime: number // 秒级时间戳
  updateTime: number // 秒级时间戳
  // 扩展字段 - 用于前端显示
  venueName?: string
  venueDescription?: string
  timeSlotName?: string
  userNickName?: string
  userAvatarUrl?: string
  bookingDateFormatted?: string
  statusText?: string
}

// 预约列表查询参数
export interface BookingListParams {
  page?: number
  size?: number
  sort?: string
  order?: 'asc' | 'desc'
  openid?: string
  phone?: string
  venueType?: VenueType
  status?: BookingStatus
  bookingDate?: string // YYYY-MM-DD格式
  timeSlot?: TimeSlot
}

// 预约列表响应
export interface BookingListResponse {
  data: Booking[]
  total: number
  code: number
  message: string
}

// 时间段可用性
export interface TimeSlotAvailability {
  timeSlot: TimeSlot
  available: boolean
  remaining: number
  timeRange: string
}

// 可用时间段信息
export interface AvailableSlots {
  venueType: VenueType
  bookingDate: string
  capacity: number
  availableSlots: Record<string, TimeSlotAvailability>
}

// 拒绝预约参数
export interface RejectBookingParams {
  reason: string
}

// 通用响应消息
export interface BookingActionResponse {
  message: string
}

export class BookingService {
  // 获取预约列表
  static getBookingList(params?: BookingListParams) {
    return request.get<BaseResponse<Booking[]>>({
      url: '/api/v1/admin/booking/list',
      params
    })
  }

  // 获取预约详情
  static getBookingDetail(bookingId: string) {
    return request.get<BaseResponse<Booking>>({
      url: `/api/v1/admin/booking/detail/${bookingId}`
    })
  }

  // 确认预约
  static confirmBooking(bookingId: string) {
    return request.put<BaseResponse<BookingActionResponse>>({
      url: `/api/v1/admin/booking/confirm/${bookingId}`
    })
  }

  // 拒绝预约
  static rejectBooking(bookingId: string, params: RejectBookingParams) {
    return request.put<BaseResponse<BookingActionResponse>>({
      url: `/api/v1/admin/booking/reject/${bookingId}`,
      params
    })
  }

  // 完成预约
  static completeBooking(bookingId: string) {
    return request.put<BaseResponse<BookingActionResponse>>({
      url: `/api/v1/admin/booking/complete/${bookingId}`
    })
  }

  // 获取场地可用时间段
  static getAvailableSlots(venueType: VenueType, bookingDate: string) {
    return request.get<BaseResponse<AvailableSlots>>({
      url: '/api/v1/admin/booking/available-slots',
      params: {
        venueType,
        bookingDate
      }
    })
  }
}

// 工具函数

// 获取场地名称
export const getVenueName = (venueType: VenueType): string => {
  const names: Record<VenueType, string> = {
    family_fishing: '亲子垂钓',
    lure_fishing: '路亚钓鱼',
    forest_yoga: '森林瑜伽'
  }
  return names[venueType] || venueType
}

// 获取场地描述
export const getVenueDescription = (venueType: VenueType): string => {
  const descriptions: Record<VenueType, string> = {
    family_fishing: '适合亲子活动的垂钓场地',
    lure_fishing: '专业路亚钓鱼场地',
    forest_yoga: '在森林中进行瑜伽练习'
  }
  return descriptions[venueType] || ''
}

// 获取时间段名称
export const getTimeSlotName = (timeSlot: TimeSlot): string => {
  const names: Record<TimeSlot, string> = {
    morning: '上午段 (09:00-12:00)',
    afternoon: '下午段 (14:00-17:00)',
    full_day: '全天段 (09:00-17:00)'
  }
  return names[timeSlot] || timeSlot
}

// 获取时间范围
export const getTimeRange = (timeSlot: TimeSlot): string => {
  const ranges: Record<TimeSlot, string> = {
    morning: '09:00-12:00',
    afternoon: '14:00-17:00',
    full_day: '09:00-17:00'
  }
  return ranges[timeSlot] || ''
}

// 获取状态文本
export const getStatusText = (status: BookingStatus): string => {
  const texts: Record<BookingStatus, string> = {
    pending: '待确认',
    confirmed: '已确认',
    cancelled: '已取消',
    rejected: '已拒绝',
    completed: '已完成'
  }
  return texts[status] || status
}

// 获取状态标签类型
export const getStatusTagType = (status: BookingStatus): string => {
  const types: Record<BookingStatus, string> = {
    pending: 'warning',
    confirmed: 'success',
    cancelled: 'info',
    rejected: 'danger',
    completed: 'primary'
  }
  return types[status] || 'info'
}

// 获取场地标签类型
export const getVenueTagType = (venueType: VenueType): string => {
  const types: Record<VenueType, string> = {
    family_fishing: 'success',
    lure_fishing: 'primary',
    forest_yoga: 'warning'
  }
  return types[venueType] || 'info'
}
